Ext.define('Jee.view.CURDGridView',{
    extend : 'Ext.grid.Panel',
    alias : ['curdGridView'],
    columnLines : true,  
    //forceFit: true,
    stripeRows: true,
    simpleSelect : true,
    modelClass:null,
    listeners:{
	    afterRender: function(){     
	    }
  },
  searchRecord:function(serviceId,condition){
     var me = this;
     var jeeRequest = {serviceId:serviceId};
     jeeRequest.data=condition||{};
     me.store.load(jeeRequest);
  },
  addRecord:function(){
	  var me = this;
	  var editWin = me.getEditWin()
      editWin.title="新增"+editWin.title;
      editWin.show();
  },
  editRecord:function(serviceId){
	  var me = this;
      var sm=me.getSelectionModel();
      if(!sm.hasSelection()){
    	   Ext.MessageBox.show({
    		   width:150,
    		   title:"错误",
    		   buttons: Ext.MessageBox.OK,
    		   msg:"请选择记录进行操作！"
		   });
    	  return;
      }
      var record =  sm.getSelection()[0];
      var id = record.get(record.idProperty);
      var jeeRequest = {serviceId:serviceId,data:id};
      var editWin = me.getEditWin()
      editWin.title="编辑"+editWin.title;
      var editWin = editWin.show();
      Jee.Ajax.request(jeeRequest,{successCallback:function(data){     
		      var form   = editWin.down('form');
		      data = Jee.Util.modelToFormObj(data);
		      var record = {data:data};
		      form.loadRecord(record); 
      },
      maskTarget:editWin
      });  
  },
  deleteRecord:function(serviceId){
      var me = this;
      var sm=me.getSelectionModel();
      if(!sm.hasSelection()){
    	   Ext.MessageBox.show({
    		   width:150,
    		   title:"错误",
    		   buttons: Ext.MessageBox.OK,
    		   msg:"请选择记录进行操作！"
		   });
    	  return;
      }
      Ext.MessageBox.confirm('确认', '您确认要删除记录？', function(btn){
    	  if (btn == 'yes')    {     
                  var rows = sm.getSelection();
			      var ids = [];
			      for(var i=0;i<rows.length;i++){
			    	  var id = rows[i].get('id');
			    	  ids.push(id);
			      }
			      var jeeRequest = {serviceId:serviceId,data:ids};
			      Jee.Ajax.request(jeeRequest,{
			    	  successCallback:function(){
			    	      me.refresh();
			    	  }
			      });
           }     
      });
  },
  searchHandler:function(){
	  this.curdToolbar.searchButton.handler();
  },
  refresh:function(){
	  	var me = this;
	  	me.pagingToolbar.doRefresh();
  },
  initComponent : function(){	
	    var me = this;
    	me.selModel=Ext.create('Ext.selection.CheckboxModel',{mode:"SIMPLE",width:20});
    	me.callParent();
    	var pagingToolbar  = Ext.create('Ext.PagingToolbar', {
		    	  dock:'bottom',
	              store: this.store,
	              displayInfo: true
	    });
    	me.pagingToolbar = pagingToolbar;
		me.addDocked(pagingToolbar);
		
		var curdToolbar = Ext.create('Jee.view.CURDToolbar',{
		    grid:me
		});
		me.curdToolbar = curdToolbar;
		me.addDocked(curdToolbar);	
		curdToolbar.searchButton.handler();
  }
});